﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Aribaccio.Core;
using Aribaccio.Logger;
using Aribaccio.Security;

namespace Aribaccio.Copyright.Client
{
    /// <summary>
    /// 程序启动类
    /// </summary>
    public class AppStartup : Singleton<AppStartup>
    {
        /// <summary>
        /// 启动容器并注册服务
        /// </summary>
        public void StartupServices()
        {
            //再向容器注册服务
            this.ConfigureServices();
        }

        /// <summary>
        /// 初始化日志服务
        /// </summary>
        public void StartupLog()
        {
            //从容器中获取-指标管理初始化
            var loggerLeader = ContainerManager.Instance.GetContainerServiceRequired<LoggerLeader>();
            loggerLeader?.Initialize();
        }

        /// <summary>
        /// 初始化加载数据与服务
        /// </summary>
        public void InitializeService()
        {
            
        }

        /// <summary>
        /// 向容器注册应用服务
        /// </summary>
        protected void ConfigureServices()
        {
            if (ContainerManager.Instance == null) return;
            //容器服务配置对象
            IConfiguration configuration = ContainerManager.Instance.Configuration;
            //容器服务注册对象
            IServiceCollection service = ContainerManager.Instance.ServiceCollection;

            // 0-注册Logger实例
            service?.AddSingleton<LoggerLeader>();

            // 2-全局容器中注册自定义服务
            service?.AddTransient<ISecurity, SecurityAES>();
            service?.AddTransient<IRegisterCopyLimits, RegisterCopyLimits>();
            
            // 4-向全局容器中注册视图模型[ViewModels]
            service?.AddSingleton<MainWindowModel>();

            // 5-向全局容器中注册视图窗体[ViewWindows]
            service?.AddSingleton(typeof(MainWindow));

            // 6-再构建容器服务对象
            ContainerManager.Instance.BuildService();
        }
    }
}
